package com.stone.front.user.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ResponseBody;

import com.stone.back.user.entity.BfCountry;
import com.stone.back.user.entity.BfUser;
import com.stone.common.util.Md5Util;
import com.stone.front.user.dao.IUserFrontDao;
import java.util.Date;
import org.apache.commons.lang.StringUtils;

/**
 * 用户管理Service
 * @author Stone P
 *
 */
@Service
public class UserFrontService implements IUserFrontService {
	@Autowired
	private IUserFrontDao userFrontDao;

	/**
	 * 查询登陆信息
	 * @param email			email
	 * @param password		密码
	 * @return				
	 */
	@Override
	public BfUser userLogin(String email, String password) {
		List<BfUser> list = this.userFrontDao.userLogin(email, password);
		BfUser user = null;
		if (0 < list.size()) {
			user = list.get(0);
		}
		return user;
	}

	/**
	 * 验证email是否存在
	 * @param email		email
	 * @return
	 */
	@Override
	public int selectUserByEmail(String email) {
		List<BfUser> list = this.userFrontDao.selectUserByEmail(email);
		return list.size();
	}

	/**
	 * 通过验证码查找用户信息
	 * @param checkCode		验证码
	 * @return
	 */
	@Override
	public Integer selectUserByCheckCode(String checkCode) {
		Integer userId = null;
		List<BfUser> list = this.userFrontDao.selectUserByCheckCode(checkCode);
		if(0 < list.size()) {
			BfUser user = list.get(0);
			userId = user.getUserid();
		}
		return userId;
	}
	
	/**
	 * 查询国家列表
	 * @return
	 */
	@Override
    public @ResponseBody List<BfCountry> selectCountry() {
		List<BfCountry> list = this.userFrontDao.selectCountry();
		return list;
	}

	/**
	 * 增加用户
	 * @param user	用户信息
	 * @return
	 */
	@Override
	public Integer addUser(BfUser user) {
		return this.userFrontDao.addUser(user);
	}

	/**
	 * 修改用户
	 * @param user	用户信息
	 * @return
	 */
	@Override
	public void updateUser(BfUser user) {
		this.userFrontDao.updateUser(user);
	}
        
        @Override
        public BfUser updateUser(Integer userid, String username, String email, String password, Date birthday, Integer sex, String address) {
            if(userid == null)
                return null;
            BfUser user = this.userFrontDao.selectUserDetail(userid);
            if(StringUtils.isNotBlank(username))
                user.setUsername(username);
            if(StringUtils.isNotBlank(email))
                user.setEmail(email);
            if(StringUtils.isNotBlank(password))
                user.setPassword(Md5Util.toMD5(password));
            if(birthday != null)
                user.setBirthday(birthday);
            if(sex != null)
                user.setSex(sex);
            if(StringUtils.isNotBlank(address))
                user.setAddress(address);
            this.userFrontDao.updateUser(user);
            
            return user;
        }

	/**
	 * 查询用户详情
	 * @param userId	用户Id
	 * @return
	 */
	@Override
	public BfUser selectUserDetail(int userId) {
		return this.userFrontDao.selectUserDetail(userId);
	}
}
